package com.example.rbac.config;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class InterfaceProxy implements InvocationHandler{
    // 获取代理对象
    public <T> T getProxy() {
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }
    private Object target;

    public InterfaceProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object object = method.invoke(target, args);
        after();
        return object;
    }

    private void before() {
        System.out.println("interface proxy before ...");
    }

    private void after() {
        System.out.println("interface proxy after ...");
    }

    public static void main(String[] args) {
        InterfaceProxy interfaceProxy = new InterfaceProxy(new HelloImpl());
        Hello proxy = interfaceProxy.getProxy();
        proxy.say();
    }
}
